package xyz.bali16.module.mute.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import xyz.bali16.application.core.enums.ApiEnum;
import xyz.bali16.application.core.model.Result;
import xyz.bali16.module.mute.entity.Mute;
import xyz.bali16.module.mute.model.MuteAddParam;
import xyz.bali16.module.mute.model.MutePageParam;
import xyz.bali16.module.mute.model.MuteUpdateParam;
import xyz.bali16.module.mute.service.MuteService;


import javax.validation.Valid;
import java.util.List;

import static xyz.bali16.application.core.model.Result.*;
import static xyz.bali16.application.core.model.Result.failure;
import static xyz.bali16.application.core.utils.ResultUtils.errorLog;

/**
 * @author bali2 on 9/3/2023.
 * @version 1.0
 */
@RestController
@RequestMapping(BaseController.API+"mute")
@Api(tags = "禁言接口")
@AllArgsConstructor

public class MuteController extends BaseController{
    public static final Logger logger = LoggerFactory.getLogger(MuteController.class);
    @Autowired
    MuteService muteService;

    @GetMapping
    @ApiOperation("分页条件查询禁言列表")
    //@PreAuthorize("@auth.permission('plugin:mute:query')")
    public Result<IPage<Mute>> pageQuery(@Valid MutePageParam param) {
        IPage<Mute> muteIPage = muteService.getPage(param);
        return success(ApiEnum.GET_SUCCESS, muteIPage);
    }

    @PostMapping
    //@PreAuthorize("@auth.permission('plugin:mute:add')")
    @ApiOperation("添加禁言")
    public Result<String> add(@RequestBody @Valid MuteAddParam param) {
        try {
            muteService.addMute(param);
            return response(ApiEnum.ADD_SUCCESS);
        } catch (Exception e) {
            errorLog(logger, e, "新增禁言 '{}' 信息失败 {}", param.getUserId(), e.getMessage());
            return failure(ApiEnum.ADD_ERROR, "新增禁言失败", e);
        }
    }

    @PutMapping
    //@PreAuthorize("@auth.permission('plugin:mute:update')")
    @ApiOperation("修改禁言")
    public Result<String> update(@RequestBody @Valid MuteUpdateParam param) {
        try {
            muteService.updateMute(param);
            return response(ApiEnum.UPDATE_SUCCESS);
        } catch (Exception e) {
            errorLog(logger, e, "更新禁言 '{}' 信息失败 {}", param.getId(), e.getMessage());
            return failure(ApiEnum.UPDATE_ERROR, "更新禁言失败", e);
        }
    }

    @DeleteMapping("/{muteId}")
    //@PreAuthorize("@auth.permission('plugin:mute:delete')")
    @ApiOperation("删除禁言")
    @ApiImplicitParam(name = "muteId", value = "禁言id", paramType = "path", required = true)
    public Result<String> deleteMute(@PathVariable("muteId") String muteId) {
        try {
            muteService.delete(muteId);
            return response(ApiEnum.DELETE_SUCCESS);
        } catch (Exception e) {
            errorLog(logger, e, "删除禁言 '{}' 失败. {}", muteId, e.getMessage());
            return failure(ApiEnum.DELETE_ERROR, e);
        }
    }

}
